package sv.gob.mtps.hr.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;


import sv.gob.mtps.hr.model.SferhEmpleado;
import sv.gob.mtps.hr.model.SferhExpediente;
import sv.gob.mtps.hr.service.RegistroExpedienteService;
import sv.gob.mtps.hr.service.RegistroPersonalService;

@Controller
public class RegistroExpedienteController {
	@Autowired
	private RegistroExpedienteService registroExpedienteService;
	
	@Autowired
	private RegistroPersonalService serviceRegistro;
	
	
	@RequestMapping(value="admin/ingresoExpediente")
	public String ingresoExpediente(@ModelAttribute("sferhExpediente") SferhExpediente sferhExpediente,BindingResult result,Map<String, Object> map,Model model,HttpServletRequest request){
		String id = (String)request.getParameter("idEmp");
		System.out.println("id de los empleados " + id);
		
		sferhExpediente.setSferhEmpleado(serviceRegistro.findSferhEmpleado(Integer.parseInt(id)));
		return "admin/ingresoExpediente";
	}
	
	
	@RequestMapping(value="admin/expedienteIngreso")
	public String expedienteIngreso(@ModelAttribute("sferhExpediente") SferhExpediente sferhExpediente,BindingResult result,Map<String, Object> map,Model model,HttpServletRequest reqeust){
	
		String id = (String) reqeust.getParameter("idEmpleado");
		
		System.out.println("id " + id);
		List data = registroExpedienteService.listSferhExpediente(Integer.parseInt(id));
		if(!data.isEmpty()){
			Iterator it = data.iterator();
			while(it.hasNext()){
				System.out.println("entra a la iteracion ");
				SferhExpediente dto  = (SferhExpediente)it.next();
				sferhExpediente.setIdExpediente(dto.getIdExpediente());
				sferhExpediente.setUbicacion(dto.getUbicacion());
				sferhExpediente.setFicheroNumero(dto.getFicheroNumero());
				System.out.println("Id expediente "  + sferhExpediente.getIdExpediente());
			}
		}
		sferhExpediente.setSferhEmpleado(serviceRegistro.findSferhEmpleado(Integer.parseInt(id)));
		return "admin/expedienteIngreso";
	}
	
	@RequestMapping(value = "/admin/showExpediente")
	public  String showExpediente(@ModelAttribute("emp") SferhEmpleado sferhEmpleado,BindingResult result,Map<String, Object> map){
		sferhEmpleado.setIdEmpleado(0);
		sferhEmpleado.setNr(null);
		sferhEmpleado.setNombreCompleto(null);
		//map.put("emp", sferhEmpleado);
		map.put("listEmp", serviceRegistro.listSferhEmpleado());
		return "admin/showExpediente";		
	}
	
	/*Se agrega o Modifica Expediente*/
	@RequestMapping(value="admin/addExpediente")
	public String addExpediente(@ModelAttribute("sferhExpediente") SferhExpediente sferhExpediente,BindingResult result,Map<String, Object> map,Model model,HttpServletRequest reqeust){
		System.out.println("entra al metoodo ");
		System.out.println("id empleado add "+sferhExpediente.getSferhEmpleado().getIdEmpleado());
		System.out.println("sale del metodo ");
		try{
			if(registroExpedienteService.listSferhExpediente(sferhExpediente.getSferhEmpleado().getIdEmpleado()).isEmpty()){
				sferhExpediente.setFechaCrea(new Date());
				registroExpedienteService.addSferhExpediente(sferhExpediente);
			}else{
				registroExpedienteService.editSferhExpediente(sferhExpediente);
			}
			
		}
		catch(DataAccessException ex){
				ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return "redirect:/admin/ingresoExpediente?idEmp="+sferhExpediente.getSferhEmpleado().getIdEmpleado();
	}

	
}
